package cc.javaee.bbs.tool;

import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Repository
public class DateUtil {
    private static final String SQLID_GETTIME = "common.get_time";


    //数据库时间
    private static Date DB_TIME = null;

    //服务器时间
    private static Date SERVER_TIME = null;

    //重置时差
    private static int RELOAD_INTERVAL = 300000;


    //重置方法
    private void reload() {
        SERVER_TIME = new Date();
    }

    //判断是否需要重置（定时重置）
    private boolean needReload(Date currentSysTime) {
        return currentSysTime.getTime() - DB_TIME.getTime() > RELOAD_INTERVAL;
    }


    public Date getTime() {
        Date currentSysTime = new Date();
        if (null == DB_TIME || needReload(currentSysTime)) {
            reload();
        }

        Date currentDbTime = new Date(DB_TIME.getTime() + currentSysTime.getTime() - SERVER_TIME.getTime());

        //db 和 server 之间时间差
        return currentDbTime;
    }

    public static String addDay(int n) {   //得到当前时间或是当前时间的前几天或是后几天
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.format(new Date());
            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(sdf.format(new Date())));
            cd.add(Calendar.DATE, n);//增加一天
//                 cd.add(Calendar.MONTH, n);//增加一个月   
            return sdf.format(cd.getTime());

        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 得到前月的第一天的时间
     */

    public Date getLMFistDateTime() {
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Date firstDate = (Date) cal_1.getTime();
        return firstDate;
    }

    /**
     * 得到前月的最后一天的时间
     */
    public Date getLMLastDateTime() {
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        Date lastDay = (Date) cale.getTime();
        return lastDay;
    }

    public String getLMLastDateMonth(int num) {

        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, num);//设置为1号,当前日期既为本月第一天
        Date lastDay = (Date) cale.getTime();
        return DateTemplate.DATE_FORMAT_MONTHTWO.format(lastDay);
    }

    /**
     * 得到当前月的第一天的时间
     */

    public Date getFistDateTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        Date firstDate = (Date) c.getTime();
        return firstDate;
    }

    /**
     * 得到当前月的最后一天的时间
     */
    public Date getLastDateTime() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date firstDate = (Date) ca.getTime();
        return firstDate;
    }
    /**
     * 推算一个月内向前或向后偏移多少天,当然推算年也可以使用
     * @param date 要被偏移的日期,<br>
     *        amout 偏移量<br>
     *        b 是否先将date格式化为yyyy-MM-dd 00:00:00 即:
     *        是否严格按整天计算
     * @return java.util.Date
     * **/
    /**
     * 推算一个小时内向前或向后偏移多少分钟,除了秒、毫秒不可以以外,其他都可以
     *
     * @param date 要被偏移的日期,<br>
     *             amout 偏移量<br>
     *             b 是否先将date格式化为yyyy-MM-dd HH:mm:00 即:
     *             是否严格按整分钟计算
     * @return java.util.Date
     **/
    public static Date addMinuteOfHour(Date date, Integer amount, Boolean b) {
        date = date == null ? new Date() : date;//如果date为null则取当前日期
        if (b) {
            SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
            try {
                date = simple.parse(simple.format(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, amount);
        return cal.getTime();
    }

    /***
     * 计算距离天数
     * @param data
     * @return String
     * @throws ParseException
     * **/
    public static long abDayNum(String mdDay) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

        if (mdDay == null || mdDay.equals("")) {
            return 0;
        }
        Date d1 = new Date();
        Date d2 = sdf.parse(mdDay);
        long daysBetween = (d2.getTime() - d1.getTime() + 1000000) / (3600 * 24 * 1000);
//			System.out.println("1987-01-01 与 2010-01-01 相隔 "+daysBetween+" 天");
        return daysBetween;
    }


    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    //获取一天的开始
    public static Date getDateInit(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }

    //获取一天的开始
    public static Date getDateInit(Long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }

}
